package main;

import java.util.ArrayList;

import events.CustomEvent;
import events.CustomListener;

public class EventManager {

	public static ArrayList<CustomListener> listeners = new ArrayList<CustomListener>();

	public synchronized static void addListener(CustomListener c) {
		listeners.add(c);
	}

	public synchronized static void removeListener(CustomListener c) {
		listeners.remove(c);
	}

	/**
	 * Fires custom event
	 * 
	 * @param evt
	 *            - event to fire
	 * @param target
	 *            - target to receive the event.
	 */
	public synchronized static void fire(CustomEvent evt,
			Class<?> target) {
		
		// Loops through listeners list
		for (CustomListener cl : listeners) {

			// Gets all interfaces of a class
			Class<?>[] itf = cl.getClass().getInterfaces();

			// Loops through interface listing and search for the matching
			// interface name
			for (Class<?> interf : itf) {
				if (interf.getSimpleName().equals(target.getSimpleName())) {
					cl.EventOccurred(evt);
					break;
				}
			}
		}
	}
}
